﻿/***************************************************************************
 *		ElGee Google Chart API Helper
 *		Copyright(C) 2011 by Laurent Geffroy lgeffroy@gmail.com
 * 
 *		This program is free software; you can redistribute it and/or 
 *		modify it under the terms of the GNU General Public License as
 *		published by the Free Software Foundation; either version 2 of 
 *		the License, or (at your option) any later version.
 *		
 *		This program is distributed WITHOUT ANY WARRANTY; without even the
 *		implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 *		See the GNU General Public License for more details.
 *	
 *      For particular licensing, please contact me at lgeffroy@gmail.com
 *****************************************************************************/


using System;
using System.Security.Cryptography;
using System.Text;
using System.Globalization;

namespace ElGee.Google.Charts
{
    /// <summary>
    /// Chart Base Class
    /// </summary>
    public class ChartBase
    {
        /// <summary>
        /// Secures the string for Javascript use on client side
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        internal string SecureString(string value)
        {
            return value.Replace("'", "\'") ;
        }

        /// <summary>
        /// Secures the double for Javascript use on client side
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        internal string SecureDouble(double value)
        {
            return value.ToString(CultureInfo.InvariantCulture).Replace(",",".");
        }

        /// <summary>
        /// Secures the SecureFloat for Javascript use on client side
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        internal string SecureFloat(float value)
        {
            return value.ToString(CultureInfo.InvariantCulture).Replace(",", ".");
        }


        /// <summary>
        /// Builds the hash key.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        internal string BuildHashKey(string value)
        {
            HMACSHA1 hash = new HMACSHA1();
            hash.Key = Encoding.ASCII.GetBytes("elgeeChartContr01");
            return Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(value))).Replace("+", "_");
        }
    }
}
